package com.Liux.blog.service.impl;

import com.Liux.blog.dao.ArticleMapper;
import com.Liux.blog.entity.Article;
import com.Liux.blog.entity.ArticleVo;
import com.Liux.blog.service.ArticleService;
import com.Liux.blog.vo.ResultStatus;
import com.Liux.blog.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.Cookie;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ArticleServiceImpl implements ArticleService {


    @Autowired
    private ArticleMapper articleMapper;


    @Override
    public List<ArticleVo> getArticle() {
        List<ArticleVo> articles = articleMapper.getArticleList();
        return articles;
    }

    @Override
    public List<Article> getTop6Article() {
        List<Article> articles = articleMapper.Top6Article();
        return articles;
    }

    @Override
    public Article getArticleById(String aid) {
        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",aid);
        List<Article> articles = articleMapper.selectByExample(example);
        return articles.get(0);
    }

    @Override
    public ResultVo createArticle(Article article) {
        article.setId(UUID.randomUUID().toString());
        article.setCreateTime(new Date());
        int i = articleMapper.insert(article);
        if(i>0){
            return new ResultVo(ResultStatus.OK,"success",null);
        }else{
            return new ResultVo(ResultStatus.NO,"fail",null);
        }
    }

}
